#ifndef SANDBOX_SDK_UTIL_PRIMITIVES_HPP
#define SANDBOX_SDK_UTIL_PRIMITIVES_HPP

namespace sandbox
{
	namespace sdk 
	{
		namespace util
		{
			template <typename T, T null, unsigned int chunksize=128>
			class async_array
			{
			public:
				async_array(){
					m_chunks=NULL;
				}
				~async_array()
				{
					if(m_chunks){
						uint32_t len=m_capacity.load(std::memory_order_relaxed);
						len/=chunksize;
						while(len){
							pool_allocator<sizeof(T)*chunksize>::free(m_chunks[--len]);
						}
					}
					gen_allocator::free(m_chunks);
				}
				template <typename ... Args)
				void push(rwlock& lock, Args ... args)
				{
					lock.lock_read();
					try{
						do {
							//capacity is guaranteed to stay contant
							uint32_t cap=m_capacity.load(std::memory_order_cst);
							uint32_t len=m_length.fetch_add(1);
							if(len>=cap)
							{
								
								void * chunk=pool_allocator<sizeof(T)*chunksize>::alloc();//may throw
								
								lock.lock_upgrade();
								
								
								
							}
						}while(true);
					}catch(...){lock.unlock_read();throw;}
					lock.unlock_read();
				}
				void remove(uint32_t index)
				{
				}
			private:
				std::atomic<uint32_t> m_length;
				std::atomic<uint32_t> m_capacity;
				void ** m_chunks;
			};
		}
	}
}
#endif //SANDBOX_SDK_UTIL_PRIMITIVES_HPP
